package org.apogee.mil.client.main.pages;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.utils.CDCUtils;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.shared.User;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.googlecode.gwt.crypto.client.TripleDesCipher;

public class ChangePwdForm extends LayoutContainer {

	private FormData formData;
	private TripleDesCipher cipher;
	private User currentUser = null;

	public ChangePwdForm() {
		this.currentUser = ServerServiceFactory.getUser(Session.getSessionId());
		cipher = new TripleDesCipher();
		cipher.setKey(CDCUtils.SECRET_KEY.getBytes());
	}

	@Override  
	protected void onRender(Element parent, int index) {  
		super.onRender(parent, index);
		createForm1();  
	}  

	private void createForm1() {  
		
		formData = new FormData("100%");
		
		FormPanel simple = new FormPanel(); 
		simple.setHeading("Change Password");  
		simple.setFrame(true);  
		simple.setWidth(400);
		simple.setLabelWidth(150);
		simple.setLabelAlign(LabelAlign.RIGHT);

		final TextField<String> currentPwd = new TextField<String>();  
		currentPwd.setFieldLabel("Current Password");  
		currentPwd.setAllowBlank(false);  
		currentPwd.setPassword(true);
		currentPwd.getFocusSupport().setPreviousId(simple.getButtonBar().getId());  
		currentPwd.setAutoWidth(true);
		simple.add(currentPwd, formData);  

		final TextField<String> newPwd = new TextField<String>();  
		newPwd.setFieldLabel("New Password");  
		newPwd.setAllowBlank(false);  
		newPwd.setPassword(true);
		newPwd.setAutoWidth(true);
		simple.add(newPwd, formData); 
		
		final TextField<String> confirmPwd = new TextField<String>();  
		confirmPwd.setFieldLabel("Confirm Password");  
		confirmPwd.setAllowBlank(false);  
		confirmPwd.setPassword(true);
		confirmPwd.setAutoWidth(true);
		simple.add(confirmPwd, formData);
		

		Button b = new Button("Change");  
		b.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				String current = currentPwd.getValue().trim();
				String pwd = newPwd.getValue().trim();
				String cfrm = confirmPwd.getValue().trim();
				
				if (!pwd.equals(cfrm)) {
					MessageBox.alert("Password Mismatch", "The new password and confirmation does not match. Please try again.", null);
					return;
				}

				try {
					pwd = cipher.encrypt(pwd);
					cfrm = cipher.encrypt(cfrm);
					current = cipher.encrypt(current);
				} catch (Exception e) {
					MessageBox.alert("Error", "Unable to change passwords", null);
					return;
				}
				ServerServiceFactory.getLoginService(currentUser.getSessionId()).changePassword(currentUser.getUserId(), current, pwd, new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						if (result.booleanValue() == true) 
							MessageBox.info("Success", "Password Changed Successfully", null);
						else
							MessageBox.alert("Error", "Password Was Not Changed", null);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("Error", "Password Was Not Changed", null);
						
					}
				});

			}
		});
		simple.addButton(b);  

		simple.setButtonAlign(HorizontalAlignment.CENTER);

		FormButtonBinding binding = new FormButtonBinding(simple);  
		binding.addButton(b);  

		add(simple); 

	}

}
